/**
 * Copyright (c) 2018 Ujued and APISP.NET. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.witalk.api;

import org.json.JSONObject;

import cc.witalk.BusinessException;
import cc.witalk.domain.Topic;
import cc.witalk.service.TopicService;
import cc.witalk.util.Messages;
import net.apisp.quick.core.annotation.Get;
import net.apisp.quick.core.annotation.Post;
import net.apisp.quick.core.annotation.Variable;
import net.apisp.quick.ioc.annotation.Autowired;
import net.apisp.quick.ioc.annotation.Controller;

/**
 * @author Ujued
 * @date 2018-06-28 22:19:12
 */
@Controller
public class TopicAPI {
	
	@Autowired
	private TopicService topicService;
	
	@Get("/topic/{id}")
	public JSONObject topic(@Variable("id") Long id) throws BusinessException {
		JSONObject data = new JSONObject();
		Topic topic = topicService.getById(id);
		data.put("title", topic.getTitle());
		data.put("content", topic.getContent());
		data.put("author", topic.getUserId());
		data.put("page_view", topic.getPageView());
		data.put("thanks", topic.getThanks());
		data.put("post_time", topic.getxTiem());
		return Messages.ok(data);
	}
	
	@Post("/topic")
	public JSONObject post(JSONObject topic) {
		// 不写了，还没实现权限框架
		Topic t = new Topic();
		t.setContent(topic.getString("content"));
		t.setThanks(topic.getLong("thanks"));
		t.setTitle(topic.getString("title"));
		return Messages.ok(null);
	}
}
